package com.lotea.android.andreaditlater.ui.widgets;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.util.AttributeSet;
import android.widget.RadioButton;

import com.lotea.android.andreaditlater.R;

public class SegmentedControlButton extends RadioButton {

	private int mCount = 0;

	private float mX;

	private static final float TEXT_SIZE = 16.0f;

	// Colors
	private int colorCheckedTop;
	private int colorCheckedBottom;
	private int colorUncheckedTop;
	private int colorUncheckedBottom;

	private int colorTextChecked;
	private int colorTextUnchecked;

	private int colorSeparator;

	private void initColors(Resources resources) {
		colorCheckedTop = resources.getColor(R.color.segmented_checked_dark);
		colorCheckedBottom = resources
				.getColor(R.color.segmented_checked_light);
		colorUncheckedTop = resources
				.getColor(R.color.segmented_unchecked_dark);
		colorUncheckedBottom = resources
				.getColor(R.color.segmented_unchecked_light);

		colorTextChecked = resources.getColor(R.color.segmented_checked_text);
		colorTextUnchecked = resources
				.getColor(R.color.segmented_unchecked_text);

		colorSeparator = resources.getColor(R.color.segmented_unchecked_text);
	}

	public SegmentedControlButton(Context context) {
		super(context);
		initColors(getResources());
	}

	public SegmentedControlButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		initColors(getResources());
	}

	public SegmentedControlButton(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		initColors(getResources());
	}

	public void setCount(int count) {
		mCount = count;
	}

	@Override
	public void onDraw(Canvas canvas) {
		int width = this.getWidth();
		int height = this.getHeight();

		// Prepare text
		String text = this.getText().toString();
		Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		float textWidth = textPaint.measureText(text);
		float textHeight = textPaint.measureText("x");

		// final float scale =
		// getContext().getResources().getDisplayMetrics().density;
		// float textSize = (int) (TEXT_SIZE * scale + 0.5f);
		textPaint.setTextSize(this.getTextSize());
		textPaint.setTextAlign(Paint.Align.CENTER);

		float canvasWidth = canvas.getWidth();

		GradientDrawable grad;

		// prepare background and text color
		if (isChecked()) {
			grad = new GradientDrawable(Orientation.TOP_BOTTOM, new int[] {
					colorCheckedTop, colorCheckedBottom });
			textPaint.setColor(colorTextChecked);
		} else {
			grad = new GradientDrawable(Orientation.TOP_BOTTOM, new int[] {
					colorUncheckedTop, colorUncheckedBottom });
			textPaint.setColor(colorTextUnchecked);
		}

		// draw background
		grad.setBounds(0, 0, width, height);
		grad.draw(canvas);

		// draw count bubble
		Drawable countDrawable = getResources().getDrawable(R.drawable.count);
		final int dHeight = countDrawable.getIntrinsicHeight();
		final int dWidth = countDrawable.getIntrinsicWidth();
		final int paddingLeft = this.getCompoundPaddingLeft();
		countDrawable.setBounds(paddingLeft, (height - dHeight) / 2, paddingLeft + dWidth, (height
				+ dHeight) / 2);
		countDrawable.draw(canvas);

		// draw text
		float w = (width / 2) - textWidth;
		float h = (height / 2) + textHeight;
		canvas.drawText(text, mX, h, textPaint);

		// draw right separator (if not the last button)
		if (this.getRight() != getResources().getDisplayMetrics().widthPixels) {
			Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
			paint.setColor(colorSeparator);
			paint.setStrokeWidth(1 / getResources().getDisplayMetrics().density);
			canvas.drawLine(width, 0, width, height, paint);
		}
	}

	@Override
	protected void onSizeChanged(int w, int h, int ow, int oh) {
		super.onSizeChanged(w, h, ow, oh);
		mX = w * 0.5f; // remember the center of the screen
	}

}